package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.jasper.tagplugins.jstl.core.Out;
import org.omg.CosNaming.NamingContextExtPackage.StringNameHelper;

import com.alibaba.fastjson.JSON;

import dao.impl.Mogujie_product_categoryDaoImpl;
import entity.Mogujie_product_category;
import entity.mogu_product;
import service.Mogujie_product_categoryService;
import service.mogu_productService;
import service.Impl.Mogujie_product_categotyServiceImpl;
import service.Impl.mogu_productServiceImpl;
import sun.util.resources.OpenListResourceBundle;

/**
 * Servlet implementation class Mogujie_product_categoryServlet
 */
@WebServlet("/Mogujie_product_categoryServlet")
public class Mogujie_product_categoryServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	Mogujie_product_categoryService mogu = null;
	mogu_productService mogupr  = null;
	@Override
	public void init() throws ServletException {
		// TODO Auto-generated method stub
//		super.init();
		mogu = new Mogujie_product_categotyServiceImpl();
		mogupr = new mogu_productServiceImpl();
	}
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Mogujie_product_categoryServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		//设置响应内容的类型并制定字符集,以免游览器展示中文内容时出现乱码
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out  = response.getWriter();
		String opr =request.getParameter("opr");
			if(opr.equals("index")) {
				List<Mogujie_product_category> list = mogu.getAllProduct();
				request.setAttribute("list", list);
				System.out.println("马上跳转");
				request.getRequestDispatcher("index.jsp").forward(request, response);
			}else if(opr.equals("list")){
				List<Mogujie_product_category> list = mogu.getAllProduct();
				request.setAttribute("list", list);
				List<mogu_product> prList = mogupr.getall();
				request.setAttribute("prList", prList);
				System.out.println("马上跳转");
				request.getRequestDispatcher("CategoryList.jsp").forward(request, response);
			}else if(opr.equals("lei")) {
				System.out.println("2612132");
				String mid = request.getParameter("id");
				Integer id=Integer.parseInt(mid);
				List<mogu_product> pr=mogupr.getmoguByid(id);
				String prjs = JSON.toJSONString(pr);
				out.print(prjs);
				out.flush();
				out.close();
			}
			
		//response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
